package com.naiterui.ehp.bs.cms.modules.devops.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.encypt.GengerCode;
import com.naiterui.ehp.bs.cms.common.utils.SMSUtil;
import com.naiterui.ehp.bs.cms.modules.devops.config.SelfInspectProperties;
import com.naiterui.ehp.bs.cms.modules.devops.entity.SysSelfInspect;
import com.naiterui.ehp.bs.cms.modules.devops.service.SelfInspectService;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectListVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectOcrVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectSmsVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectVO;
import com.naiterui.ehp.bs.cms.modules.user.service.IDoctorService;
import com.naiterui.ehp.bs.cms.modules.user.vo.OcrParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author luao
 * @date 2022/4/21 4:06 PM
 **/
@Api(tags = {SwaggerTag.CMS + SwaggerTag.SELF_INSPECT})
@RestController
@RequiredArgsConstructor
@RequestMapping("/devops/self/inspect")
@PreAuthorize("@p.check('devops:self:inspect')")
public class SelfInspectController {

    private final SelfInspectService selfInspectService;
    private final IDoctorService doctorService;
    private final SelfInspectProperties selfInspectProperties;

    @SysLog("系统自检列表")
    @ApiOperation(value = "系统自检列表")
    // @PreAuthorize("@p.check('devops:self:inspect:list')")
    @GetMapping("list")
    public List<SelfInspectListVO> list(Integer status) {
        return selfInspectService.list(status);
    }

    @SysLog("系统自检成功")
    @ApiOperation(value = "系统自检成功")
    @PutMapping("/success/{id}")
    // @PreAuthorize("@p.check('devops:self:inspect:success')")
    public void selfInspectSuccess(@PathVariable Long id) {
        selfInspectService.selfInspectSuccess(id);
    }

    @SysLog("微信服务号自检")
    @ApiOperation(value = "微信服务号自检")
    @GetMapping("/wx/mp")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO wxMpCheck() throws BusinessException {
        return selfInspectService.wxMpCheck();
    }

    @SysLog("微信小程序自检")
    @ApiOperation(value = "微信小程序自检")
    @GetMapping("/wx/ma")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO wxMaCheck() throws BusinessException {
        return selfInspectService.wxMaCheck();
    }

    @SysLog("微信商户号自检")
    @ApiOperation(value = "微信商户号自检")
    @GetMapping("/wx/mch")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO wxMchCheck() throws BusinessException {
        return selfInspectService.wxMchCheck();
    }

    @SysLog("短信自检")
    @ApiOperation(value = "短信自检")
    @GetMapping("/sms")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO smsCheck(String phone) throws BusinessException {
        SelfInspectSmsVO result = SelfInspectSmsVO.builder().appid(selfInspectProperties.getCloopenAppid())
                .sid(selfInspectProperties.getCloopenSid()).token(selfInspectProperties.getCloopenToken())
                .templateId(selfInspectProperties.getCloopenTemplateId()).build();
        try {
            String smsCode = GengerCode.generateSmsCode();
            SMSUtil.sendSMS(phone, SMSUtil.Template.USER_LOGIN_SMS_CDOE, smsCode);
        } catch (Exception e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @SysLog("友盟自检")
    @ApiOperation(value = "友盟自检")
    @GetMapping("/push")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO pushCheck() throws BusinessException {
        return selfInspectService.pushCheck();
    }

    @SysLog("腾讯云实时音视频自检")
    @ApiOperation(value = "腾讯云实时音视频自检")
    @GetMapping("/trtc")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO trtcCheck() throws BusinessException {
        return selfInspectService.trtcCheck();
    }

    @SysLog("腾讯云人脸核身自检")
    @ApiOperation(value = "腾讯云人脸核身自检")
    @GetMapping("/txFace")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO txFaceCheck() throws BusinessException {
        return selfInspectService.txFaceCheck();
    }

    @SysLog("E签宝自检")
    @ApiOperation(value = "E签宝自检")
    @GetMapping("/esign")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO esignCheck() throws BusinessException {
        return selfInspectService.esignCheck();
    }

    @SysLog("四川CA自检")
    @ApiOperation(value = "四川CA自检")
    @GetMapping("/scca")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO sccaCheck() throws BusinessException {
        return selfInspectService.sccaCheck();
    }

    @SysLog("快递100自检")
    @ApiOperation(value = "快递100自检")
    @GetMapping("/kuaidi100")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO kuaidi100Check(String num, String companyCode) throws BusinessException {
        return selfInspectService.kuaidi100Check(num, companyCode);
    }

    @SysLog("阿里云OCR自检")
    @ApiOperation(value = "阿里云OCR自检")
    @PostMapping("/ocr")
    // @PreAuthorize("@p.check('devops:self:inspect')")
    public SelfInspectVO ocrCheck(@RequestBody OcrParamVO ocrParamVO) throws BusinessException {
        SelfInspectOcrVO result = SelfInspectOcrVO.builder().appcode(selfInspectProperties.getAliyunOcrAppcode())
                .customizeHost(selfInspectProperties.getAliyunOcrCustomizeHost())
                .customizeMethod(selfInspectProperties.getAliyunOcrCustomizeMethod())
                .customizePath(selfInspectProperties.getAliyunOcrCustomizePath())
                .customizePractice(selfInspectProperties.getAliyunOcrCustomizePractice())
                .customizeQualifications(selfInspectProperties.getAliyunOcrCustomizeQualifications())
                .idcardHost(selfInspectProperties.getAliyunOcrIdcardHost())
                .idcardMethod(selfInspectProperties.getAliyunOcrIdcardMethod())
                .idcardPath(selfInspectProperties.getAliyunOcrIdcardPath()).build();
        try {
            doctorService.ocrDistinguish(ocrParamVO);
            // 自检成功更新自检状态
            selfInspectService.selfInspectSuccessByType(SysSelfInspect.TYPE_OCR);
        } catch (BusinessException e) {
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }
}
